<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *会员列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'员工活码列表'),
	array('op'=>'edit','title'=>'添加员工活码'),
);
$return['types']=array(
	'0'=>'文本',
	'1'=>'图片',
	'2'=>'语音（音乐）',
	'3'=>'视频',
	'4'=>'图文',
);
//会员列表
if($_RQ['op']=='list'){
	$params=array('merchid'=>$_SESSION['onemerchid']);
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	if(!empty($_RQ['name'])){
		$params2=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$_SESSION['onemerchid']);
		$params2['name like']='%'.trim($_RQ['name']).'%';
		$workerids=pdo_getall('jy_scrm_worker',$params2,array('id'),'id');
		if($workerids){
			$workerids=array_keys($workerids);
		}else{
			$workerids=-1;
		}
		$params['workerid']=$workerids;
		$return['params']['name']=$_RQ['name'];
	}
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$list=pdo_getslice('jy_worker_ewm_worker',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['name']=pdo_getcolumn('jy_scrm_worker',array('id'=>$item['workerid'],'uniacid'=>$_SESSION['uniacid']),'name');
			$item['member']=array('mobile'=>$item['alias'],'avatar'=>$item['avatar'],'nickname'=>$item['name']);
			$item['url']=custom_url('jy_merchscrm/merch.gochat/worker/id='.$item['id'],true);
			$item['iscodenum']=	pdo_count('jy_worker_ewm_list',array('uniacid'=>$_SESSION['uniacid'],'workerid'=>$item['id'],'merchid'=>$_SESSION['onemerchid']));
			if(!$item['ewm']){
				$item['getewm']=1;
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：
		</br>1、<red>这里添加企业员工的二维码信息！</red>
		</br>2、<red>企业微信客户跟公众号粉丝互通，才能锁定跳转！</red>，需要开启微信三方开放平台：https://open.weixin.qq.com/，<red>如何设置？如何绑定微信开发者ID?</red>：https://developer.work.weixin.qq.com/document/path/92114#%E5%A6%82%E4%BD%95%E7%BB%91%E5%AE%9A%E5%BE%AE%E4%BF%A1%E5%BC%80%E5%8F%91%E8%80%85id

		','del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'id','title'=>'ID','type'=>'string'),
			array('name'=>'member','title'=>'成员信息','type'=>'member'),
			array('name'=>'ewm','title'=>'加好友二维码','type'=>'image'),
			array('name'=>'ewmurl','title'=>'二维码链接','type'=>'copy'),
			array('name'=>'url','title'=>'加好友链接','type'=>'copy'),
			array('name'=>'iscodenum','title'=>'已关联的活码数','type'=>'string'),
			array('type'=>'href','action'=>'jy_merchscrm/merch.worker.gift//ishuo=1','title'=>'加好友送礼设置'),
			array('type'=>'href','action'=>'jy_merchscrm/merch.worker.giftlog//ishuo=1','title'=>'送礼记录'),
		),'action'=>array(
			array('type'=>'post','op'=>'getewm','title'=>'生成加好友二维码','show'=>array('getewm'=>1)),
			array('type'=>'href','op'=>'setewm','title'=>'设置员工二维码链接'),
		)),
		'search'=>array(
			array('name'=>'name','title'=>'员工名称','type'=>'string','width'=>'15'),
		),	
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='setewm'){
	$return=array();
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$id=intval($post['id']);
		$data=array(
			'ewm'=>$post['ewm'],
			'ewmurl'=>$post['ewmurl'],
		);
		pdo_update('jy_worker_ewm_worker',$data,array('id'=>$id));
		exi('设置成功！','','goback');
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_worker_ewm_worker',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$worker=pdo_get('jy_scrm_worker',array('id'=>$item['workerid'],'uniacid'=>$_SESSION['uniacid']),array('userid','name'));
		if(!$item['ewm']){
			$item['ewm']=model('jy_scrm/worker')->get_ewm($worker['userid'],$worker['name'],'jy_scrm|jy_worker_ewm|'.$item['workerid']);
			if($item['ewm']){
				$this->pdo_update('worker',array('ewm'=>$item['ewm']),array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'edit'=>array(
			array('type'=>'string','title'=>'加员工二维码链接地址','name'=>'ewmurl','help'=>'把上面的加员工二维码，通过https://jiema.wwei.cn/在线解码，把解码到的链接，填入此处！这样方便点击链接实现联系员工！'),					
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_worker_ewm_worker',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回成员列表'),
		),
		'edit'=>array(
			array('type'=>'choose','title'=>'所属员工','name'=>'workerid','action'=>'jy_merchscrm/merch.worker.get_worker/'),
			array('type'=>'text','title'=>'前端描述','name'=>'desc','help'=>''),
			array('name'=>'linktitle','title'=>'跳转链接名称','type'=>'string','help'=>'如果不填就不弹出；客户扫活码加本员工好友后，会自动弹出【跳转链接】相关信息，方便客户自助点击进入真实的链接地址！'),
			array('name'=>'linkdesc','title'=>'跳转链接描述','type'=>'string'),
			array('name'=>'linkimg','title'=>'跳转链接图片','type'=>'image'),
			array('name'=>'link','title'=>'跳转链接地址','type'=>'link','from'=>'action','help'=>'优先跳转活码本身的目标地址，如果活码本身的没用目标地址，这个地址才生效！'),
			array('type'=>'choose','name'=>'welcomeid','title'=>'其他欢迎语','action'=>'jy_merchscrm/merch.get_welcome/','choose_title'=>'选择欢迎语','help'=>''),
			array('name'=>'shoplevel_id','title'=>'绑定商城会员等级','type'=>'select','options'=>'shoplevels'),
		),
	);	
	$return['shoplevels']=pdo_getall('jy_weishop_member_level',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	//添加默认等级
	$return['shoplevels'][0]=array(
			'id'=>'0',
			'title'=>'默认会员',
			'level'=>0,
			'discount'=>'0',
			'ordermoney'=>'0.00',
			'ordercount'=>'0',
			'status'=>'1',
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'workerid'=>trim($postdata['workerid']),
		'desc'=>trim($postdata['desc']),
		'linktitle'=>trim($postdata['linktitle']),
		'linkdesc'=>trim($postdata['linkdesc']),
		'linkimg'=>trim($postdata['linkimg']),
		'link'=>trim($postdata['link']),
		'welcomeid'=>intval($postdata['welcomeid']),
		'shoplevel_id'=>intval($postdata['shoplevel_id']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_worker_ewm_worker',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['merchid']=$_SESSION['onemerchid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_worker_ewm_worker',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='getewm'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_worker_ewm_worker',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$worker=pdo_get('jy_scrm_worker',array('id'=>$item['workerid'],'uniacid'=>$_SESSION['uniacid']),array('userid','name'));
		if(!$item['ewm']){
			$item['ewm']=model('jy_scrm/worker')->get_ewm($worker['userid'],$worker['name'],'jy_scrm|jy_worker_ewm|'.$item['workerid']);
			if($item['ewm']){
				$this->pdo_update('worker',array('ewm'=>$item['ewm']),array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}else{
		exi('请选择员工！','error');
	}
	exi('生成成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_worker_ewm_worker',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}